package common.model;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import javax.faces.FacesException;

/**
 *
 * @author shousuke
 */
public abstract class GenericEntityBean {

    public abstract Long getId();

    public abstract void setId(Long id);

    public abstract Date getCreatedDate();

    public abstract void setCreatedDate(Date createdDate);

    public abstract Date getModifiedDate();

    public abstract void setModifiedDate(Date modifiedDate);

    public abstract String getOprtnlFlag();

    public abstract void setOprtnlFlag(String oprtnlFlag);

    @Override
    public int hashCode() {
        int hash = 0;
        Long id = getId();
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object.getClass() == getClass())) {
            return false;
        }
        Long id = getId();
        GenericEntityBean other = (GenericEntityBean) object;
        Long otherId = other.getId();
        if ((id == null && otherId != null) || (id != null && !id.equals(otherId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        Class<? extends GenericEntityBean> c = getClass();
        StringBuffer sb = new StringBuffer("\n" + c.getName() + "\n");
        Method[] m = c.getMethods();
        String delim = "get";
        for (Method i : m) {
            String mn = i.getName();
            if (mn.startsWith(delim)) {
                String fn = mn.substring(mn.indexOf(delim) + delim.length());
                fn = fn.substring(0, 1).toLowerCase() + fn.substring(1);
                try {
                    sb.append("\t" + fn + " = " + i.invoke(this) + "\n");
                } catch (IllegalAccessException e) {
                    throw new FacesException(e);
                } catch (InvocationTargetException e) {
                    throw new FacesException(e);
                }
            }
        }
        return sb.toString();
    }
}
